//
//  BRootViewController.m
//  UI 豆瓣完整版
//
//  Created by dllo on 16/1/9.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "BRootViewController.h"
#import "MyView.h"
#import "MovieModel.h"
#import "UIImageView+WebCache.h"
#import "lyfDataBaseHandle.h"

@interface BRootViewController ()

@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) NSMutableArray *arr;

@end

@implementation BRootViewController

- (void)dealloc {
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 创建滚动视图
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    _scrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_scrollView];

    
    // 设置滚动范围
    _scrollView.contentSize = CGSizeMake(0, self.view.frame.size.height * 3);

    MyView *view1 = [[[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil] lastObject];
    [_scrollView addSubview:view1];
    
    view1.label1.text = self.model.title;
    NSString *str = [_model.begin_time substringWithRange:NSMakeRange(5, 11) ];
    NSString *str1 = [_model.end_time substringWithRange:NSMakeRange(5, 11)];
    NSString *Str = [NSString stringWithFormat:@"%@-%@", str, str1];
    view1.label2.text = Str;
    view1.label3.text = self.model.name;
    view1.label4.text = [NSString stringWithFormat:@"类型：%@", _model.category_name];
    view1.label5.text = self.model.address;
    view1.bigLabel.text = self.model.content;
    view1.bigLabel.numberOfLines = 1000;
    [view1.bigImg sd_setImageWithURL:[NSURL URLWithString:_model.image]];
    
    
    
    
    
    UIBarButtonItem *item0 = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"btn_nav_back@2x.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(buttonAction:)];
    self.navigationItem.title = self.model.title;
    self.navigationItem.leftBarButtonItem = item0;
    
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"btn_nav_share@2x.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(button1Action:)];
    self.navigationItem.rightBarButtonItem = item1;
}

- (void)buttonAction:(UIBarButtonItem *)button {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)button1Action:(UIBarButtonItem *)button1 {
    

    [[lyfDataBaseHandle sharedDataBaseHandle] openDB];
    self.arr = [[lyfDataBaseHandle sharedDataBaseHandle] selectTable];

    if ([_arr containsObject:self.model.title]) {
        UIAlertController *alert1 = [UIAlertController alertControllerWithTitle:@"该活动已收藏过" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleCancel handler:nil];
        [alert1 addAction:action1];
        [self presentViewController:alert1 animated:YES completion:nil];
        
    } else {
        UIAlertController *alert0 = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否收藏" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action0 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            UIAlertController *alert1 = [UIAlertController alertControllerWithTitle:@"已收藏" message:nil preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleCancel handler:nil];
            [alert1 addAction:action1];
            [[lyfDataBaseHandle sharedDataBaseHandle] insertModel:self.model];
            [self presentViewController:alert1 animated:YES completion:nil];
        }];
        UIAlertAction *action00 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        [alert0 addAction:action0];
        [alert0 addAction:action00];
        [self presentViewController:alert0 animated:YES completion:nil];
    }
    
 
    }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
